<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中文名字生成器</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .input-group {
            margin-bottom: 20px;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .result-container {
            margin-top: 30px;
        }
        .name-card {
            background-color: #f9f9f9;
            border: 1px solid #eee;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
        }
        .chinese-name {
            font-size: 24px;
            color: #333;
            margin-bottom: 10px;
        }
        .meaning {
            color: #666;
            line-height: 1.5;
        }
        .loading {
            text-align: center;
            display: none;
        }
        .error-message {
            color: #ff0000;
            text-align: center;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>中文名字生成器</h1>
        <div class="input-group">
            <input type="text" id="englishName" placeholder="请输入您的英文名" />
        </div>
        <button id="generateBtn" onclick="generateNames()">生成中文名字</button>
        <div class="loading" id="loading">生成中，请稍候...</div>
        <div class="error-message" id="errorMessage"></div>
        <div class="result-container" id="resultContainer"></div>
    </div>

    <script>
        const API_KEY = 'heyhey';
        const API_URL = 'https://ark.cn-beijing.volces.com/api/v3/chat/completions';

        async function generateNames() {
            const englishName = document.getElementById('englishName').value.trim();
            if (!englishName) {
                showError('请输入英文名');
                return;
            }

            const generateBtn = document.getElementById('generateBtn');
            const loading = document.getElementById('loading');
            const resultContainer = document.getElementById('resultContainer');
            const errorMessage = document.getElementById('errorMessage');

            generateBtn.disabled = true;
            loading.style.display = 'block';
            errorMessage.style.display = 'none';
            resultContainer.innerHTML = '';

            const prompt = `请为英文名"${englishName}"生成3个有趣的中文名字。每个名字都应该：
1. 体现中国文化特色
2. 包含名字的中英文含义解释
3. 可以适当加入有趣的梗
4. 确保名字朗朗上口且寓意美好

请按以下格式返回：
中文名1：[名字]
寓意：[中文解释]
Meaning：[英文解释]

中文名2：[名字]
寓意：[中文解释]
Meaning：[英文解释]

中文名3：[名字]
寓意：[中文解释]
Meaning：[英文解释]`;

            try {
                const controller = new AbortController();
                const timeoutId = setTimeout(() => controller.abort(), 60000);

                const response = await fetch(API_URL, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${API_KEY}`
                    },
                    body: JSON.stringify({
                        model: 'deepseek-r1-250120',
                        messages: [
                            {role: 'system', content: '你是一个专业的中文起名专家，精通中国文化和起名艺术。'},
                            {role: 'user', content: prompt}
                        ]
                    }),
                    signal: controller.signal
                });

                clearTimeout(timeoutId);

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                const names = data.choices[0].message.content;
                
                // 将返回的文本按名字分割并展示
                const nameGroups = names.split('\n\n');
                let html = '';
                
                nameGroups.forEach(group => {
                    if (group.trim()) {
                        const lines = group.split('\n');
                        html += `
                            <div class="name-card">
                                <div class="chinese-name">${lines[0]}</div>
                                <div class="meaning">
                                    ${lines[1] || ''}<br>
                                    ${lines[2] || ''}
                                </div>
                            </div>`;
                    }
                });

                resultContainer.innerHTML = html;
            } catch (error) {
                if (error.name === 'AbortError') {
                    showError('请求超时，请重试');
                } else {
                    showError('生成名字时出错，请重试');
                }
                console.error('Error:', error);
            } finally {
                generateBtn.disabled = false;
                loading.style.display = 'none';
            }
        }

        function showError(message) {
            const errorMessage = document.getElementById('errorMessage');
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }
    </script>
</body>
</html>